package com.ruoyi.web.controller.business;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.HurtPatientDoctor;
import com.ruoyi.business.service.IHurtDoctorArticleService;
import com.ruoyi.business.service.IHurtPatientDoctorService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.HurtDoctorMessage;
import com.ruoyi.business.service.IHurtDoctorMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 医师信息Controller
 *
 * @author ruoyi
 * @date 2024-04-17
 */
@RestController
@RequestMapping("/business/doctorMessage")
public class HurtDoctorMessageController extends BaseController
{
    @Autowired
    private IHurtDoctorMessageService hurtDoctorMessageService;

    @Autowired
    private IHurtPatientDoctorService hurtPatientDoctorService;

    @Autowired
    private IHurtDoctorArticleService hurtDoctorArticleService;

    /**
     * 查询医师信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:doctorMessage:list')")
    @GetMapping("/list")
    public TableDataInfo list(HurtDoctorMessage hurtDoctorMessage)
    {
        startPage();
        List<HurtDoctorMessage> list = hurtDoctorMessageService.selectHurtDoctorMessageList(hurtDoctorMessage);
        return getDataTable(list);
    }

    /**
     * 导出医师信息列表
     */
    @PreAuthorize("@ss.hasPermi('business:doctorMessage:export')")
    @Log(title = "医师信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HurtDoctorMessage hurtDoctorMessage)
    {
        List<HurtDoctorMessage> list = hurtDoctorMessageService.selectHurtDoctorMessageList(hurtDoctorMessage);
        ExcelUtil<HurtDoctorMessage> util = new ExcelUtil<HurtDoctorMessage>(HurtDoctorMessage.class);
        util.exportExcel(response, list, "医师信息数据");
    }

    /**
     * 获取医师信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:doctorMessage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hurtDoctorMessageService.selectHurtDoctorMessageById(id));
    }

    /**
     * 新增医师信息
     */
    @PreAuthorize("@ss.hasPermi('business:doctorMessage:add')")
    @Log(title = "医师信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HurtDoctorMessage hurtDoctorMessage)
    {
        return toAjax(hurtDoctorMessageService.insertHurtDoctorMessage(hurtDoctorMessage));
    }

    /**
     * 修改医师信息
     */
    @PreAuthorize("@ss.hasPermi('business:doctorMessage:edit')")
    @Log(title = "医师信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HurtDoctorMessage hurtDoctorMessage)
    {
        return toAjax(hurtDoctorMessageService.updateHurtDoctorMessage(hurtDoctorMessage));
    }

    /**
     * 删除医师信息
     */
    @PreAuthorize("@ss.hasPermi('business:doctorMessage:remove')")
    @Log(title = "医师信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hurtDoctorMessageService.deleteHurtDoctorMessageByIds(ids));
    }

    /**
     * 获取医师列表
     * @return
     */
    @GetMapping("/getDoctorList")
    public TableDataInfo getDoctorList(@RequestParam(required = false) String doctorName){
        System.out.println("doctorName = " + doctorName);
        //患者id
        Long patientId = SecurityUtils.getUserId();
        //获取患者医生绑定关系
        HurtPatientDoctor hurtPatientDoctor = hurtPatientDoctorService.selectHurtPatientDoctorByPatientId(patientId);

        startPage();
        //获取所有医师列表
        List<Map<String, Object>> doctorList = hurtDoctorMessageService.getDoctorList(doctorName);

        //如果医师与患者已绑定，就修改isBing字段值
        if (hurtPatientDoctor!=null){
            for (Map<String, Object> map : doctorList) {
                Long doctorId = Long.valueOf(map.get("doctorId").toString());
                if (doctorId.equals(hurtPatientDoctor.getDoctorId())){
                    map.put("isBind",1);
                }
            }
        }
        return getDataTable(doctorList);
    }

    /**
     * 获取医师详情及相关发布
     * @param doctorId
     * @return
     */
    @GetMapping("/getDoctorDetail")
    public AjaxResult getDoctorDetail(@RequestParam Long doctorId){
        System.out.println("doctorId = " + doctorId);
        //患者id
        Long patientId = SecurityUtils.getUserId();
        //获取患者医生绑定关系
        HurtPatientDoctor hurtPatientDoctor = hurtPatientDoctorService.selectHurtPatientDoctorByPatientId(patientId);
        //医师信息
        Map<String, Object> doctorDetail = hurtDoctorMessageService.getDoctorDetail(doctorId);
        //医师相关发布
        List<Map<String, Object>> articleList = hurtDoctorArticleService.getDoctorArticleListByDoctorId(doctorId);
        if (doctorDetail!=null){
            if (hurtPatientDoctor!=null&& doctorId.equals(hurtPatientDoctor.getDoctorId())){
                doctorDetail.put("isBind",1);
            }
            doctorDetail.put("articleList",articleList);
        }
        return AjaxResult.success(doctorDetail);
    }
}
